package leetcode_字符串._10字符串变换;

import java.util.ArrayList;

/**
 * z 字形变换
 * 算法 :
 *   abcd 3
 *   a   e
 *   b d f
 *   c   g
 *
 */
public class _6 {

    public String convert(String s, int numRows) {
        if (s.length() < numRows || numRows == 1) {
            return s;
        }
        ArrayList<StringBuilder> list = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuilder());
        }
        int x = 0, flag = 1;
        for (int i = 0; i < s.length(); i++) {
            list.get(x).append(s.charAt(i));
            if (x == 0) {
                flag = 1;
            } else if (x == numRows - 1) {
                flag = -1;
            }
            x += flag;
        }
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            ans.append(list.get(i));
        }
        return ans.toString();
    }
}
